package com.mi.rof.engineer.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mi.rof.engineer.mapper.InventoryApplyMapper;
import com.mi.rof.engineer.mapper.PaymentMapper;
import com.mi.rof.entity.InventoryApplyTable;
import com.mi.rof.entity.PaymentTable;
import com.mi.rof.entity.RepairOrder;
import com.mi.rof.engineer.mapper.RepairOrderMapper;
import com.mi.rof.engineer.service.RepairOrderService;
import com.mi.rof.entity.SystemException;
import com.mi.rof.entity.enums.ExceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;


@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class RepairOrderServiceImpl implements RepairOrderService {

    @Resource
    private RepairOrderMapper repairOrderMapper;

    @Resource
    private InventoryApplyMapper inventoryApplyMapper;

    @Resource
    private PaymentMapper paymentMapper;


    @Override
    public RepairOrder findRepairOrderById(String orderId) {
        LambdaQueryWrapper<RepairOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RepairOrder::getRepairOrderId , orderId);
        return repairOrderMapper.selectOne(queryWrapper);
    }

    @Override

    public Integer updateRepairOrder(RepairOrder repairOrder) {
        LambdaQueryWrapper<RepairOrder> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(RepairOrder::getRepairOrderId, repairOrder.getRepairOrderId());
        return repairOrderMapper.update(repairOrder,queryWrapper);

    }

    @Override
    public BigDecimal calculateTotalPrice(String repairOrderId) {
        // 根据repairOrderId批量查询
        List<InventoryApplyTable> inventoryApplyTables = inventoryApplyMapper.selectList(new LambdaQueryWrapper<InventoryApplyTable>().eq(InventoryApplyTable::getRepairOrderId, repairOrderId));
        // 计算价格
        BigDecimal totalPrice = BigDecimal.valueOf(0.0);
        for (InventoryApplyTable inventoryApplyTable : inventoryApplyTables) {
            BigDecimal quantity =  new BigDecimal(inventoryApplyTable.getQuantity());
            //加和
            totalPrice = totalPrice.add(inventoryApplyTable.getUnitPrice().multiply(quantity));
        }
        return totalPrice;
    }

    @Override
    public Integer insertPaymentTable(PaymentTable paymentTable) {
        int result=0;
        try{
             result = paymentMapper.insert(paymentTable);
        } catch (Exception e) {
            throw new SystemException(ExceptionEnum.INSERT_PAYMENT_TABLE_ERROR.getCode(),
                    ExceptionEnum.INSERT_PAYMENT_TABLE_ERROR.getMsg());
        }

        return result;
    }

    @Override
    public PaymentTable findPaymentTableByRepairOrderId(String repairOrderId) {
        PaymentTable paymentTable = null;
        try {
            paymentTable = paymentMapper.selectOne(new LambdaQueryWrapper<PaymentTable>()
                    .eq(PaymentTable::getRepairOrderId, repairOrderId));
        } catch (Exception e) {
            throw new SystemException(ExceptionEnum.PAYMENT_ORDER_NOT_EXIST.getCode(),
                    ExceptionEnum.PAYMENT_ORDER_NOT_EXIST.getMsg());
        }
        return paymentTable;
    }

    @Override
    public Integer updatePaymentState(PaymentTable paymentTable) {
        try {
            paymentMapper.update(paymentTable,new LambdaQueryWrapper<PaymentTable>()
                    .eq(PaymentTable::getRepairOrderId,paymentTable.getRepairOrderId()));
        } catch (Exception e) {
            throw new SystemException(ExceptionEnum.UPDATE_ERROR.getCode(),
                    ExceptionEnum.UPDATE_ERROR.getMsg());
        }
        return 0;
    }

}
